package com.pcg.posgasstation.model.network.jsonadapter;

import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.pcg.posgasstation.model.network.entity.BaseEntity;
import com.pcg.posgasstation.model.network.entity.StaffInfoEntity;
import com.pcg.posgasstation.utils.LogUtil;

import java.lang.reflect.Type;

public class StaffInfoJsonAdapter implements JsonDeserializer<BaseEntity<StaffInfoEntity>> {

    @Override
    public BaseEntity<StaffInfoEntity> deserialize(final JsonElement json, final Type typeOfT,
                                                   final JsonDeserializationContext context)
            throws JsonParseException {
        BaseEntity options = null;
        Type listType = new TypeToken<BaseEntity<StaffInfoEntity>>() {
        }.getType();
        try {
            options = new Gson().fromJson(json, listType);
        } catch (JsonSyntaxException e) {
            LogUtil.e("StaffInfoJsonAdapter", "JsonSyntaxException");
            e.printStackTrace();
            JsonObject jsonObject = json.getAsJsonObject();

            if (jsonObject.has("data")) {
                jsonObject.remove("data");
                options = new Gson().fromJson(json, listType);
            }
        }

        return options;
    }

}